//
//  TSCircularProgressView.swift
//  Pods
//
//  Created by 100Years on 2025/3/21.
//

import UIKit

open class TSCircularProgressView: UIView {
    // MARK: - Properties
    public var trackLayer: CAShapeLayer!
    public var progressLayer: CAShapeLayer!
    
    /// 进度条颜色
    public var progressColor: UIColor = .blue {
        didSet {
            progressLayer.strokeColor = progressColor.cgColor
            //print("Progress Color Updated: \(progressColor)")
        }
    }
    
    /// 背景轨道颜色
    public var trackColor: UIColor = .lightGray {
        didSet {
            trackLayer.strokeColor = trackColor.cgColor
        }
    }
    
    /// 线条粗细
    public var lineWidth: CGFloat = 10.0 {
        didSet {
            trackLayer.lineWidth = lineWidth
            progressLayer.lineWidth = lineWidth
            setNeedsLayout()
        }
    }
    
    /// 进度（0.0 到 1.0）
    public var progress: CGFloat = 0.0 {
        didSet {
            progress = min(max(progress, 0.0), 1.0) // 限制范围
//            print("Progress Updated: \(progress)")
            progressLayer.strokeEnd = progress
        }
    }
    
    /// 绘制方向（默认顺时针）
    public var isClockwise: Bool = true {
        didSet {
            setNeedsLayout()
        }
    }
    
    // MARK: - Initialization
    override public init(frame: CGRect) {
        super.init(frame: frame)
        setupLayers()
    }
    
    required public init?(coder: NSCoder) {
        super.init(coder: coder)
        setupLayers()
    }
    
    // MARK: - Setup Layers
    public func setupLayers() {
        // 背景轨道
        trackLayer = CAShapeLayer()
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.strokeColor = trackColor.cgColor
        trackLayer.lineWidth = lineWidth
        trackLayer.lineCap = .round
        layer.addSublayer(trackLayer)
        
        // 进度条
        progressLayer = CAShapeLayer()
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = progressColor.cgColor
        progressLayer.lineWidth = lineWidth
        progressLayer.lineCap = .round
        progressLayer.strokeEnd = progress
        layer.addSublayer(progressLayer)
        
        //print("Progress Layer Added: \(progressLayer)")
    }
    
    // MARK: - Layout
    override public func layoutSubviews() {
        super.layoutSubviews()
        //print("layoutSubviews Called")
        updatePaths()
    }
    
    public func updatePaths() {
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = min(bounds.width, bounds.height) / 2 - lineWidth / 2
        
        // 背景轨道路径
        let trackPath = UIBezierPath(
            arcCenter: center,
            radius: radius,
            startAngle: -.pi / 2, // 从顶部开始
            endAngle: .pi * 1.5,   // 到顶部结束
            clockwise: true // 背景轨道始终为顺时针
        )
        trackLayer.path = trackPath.cgPath
        
        // 进度条路径
        let progressPath = UIBezierPath(
            arcCenter: center,
            radius: radius,
            startAngle: -.pi / 2, // 从顶部开始
            endAngle: -.pi / 2 + .pi * 2 * (isClockwise ? 1 : -1), // 根据 isClockwise 控制方向
            clockwise: isClockwise
        )
        progressLayer.path = progressPath.cgPath
        //print("Progress Layer Path: \(progressLayer.path)")
    }
}
